set more off

use dataset, clear

* program for substantive effects
capture program drop myest
program define myest
	
	quietly{
	
		args model x nreps
		gen B`model' = .
		gen SE`model' = .
		gen EB`model' = .
		gen ESE`model' = .
		
		// coef estimate
		local myX = subinstr("`e(cmdline)'", "regress ", "", 1)
		reg `myX'

		replace B`model' = _b[`x'] if _n == `model'
		replace SE`model' = _se[`x'] if _n == `model'

		
		// coef SE
		
		// subs effect point estimate
		preserve
			replace `x' = 0
			predict yhat, xb
			su yhat
		restore
		local y0 = r(mean)
		preserve
			replace `x' = 1
			predict yhat, xb
			su yhat
		restore
		local y1 = r(mean)
		replace EB`model' = exp(`y1')-exp(`y0') if _n == `model' 
		// subs effect standard error
		capture erase tmp0.dta
		gen B = .
		save tmp0, replace
		nois _dots 0, title(Loop running) reps(`nreps')
		forvalues i = 1(1)`nreps' {
			use tmp0, clear
			bsample
			local myX = subinstr("`e(cmdline)'", "regress ", "", 1)
			preserve
				qui: reg `myX'
			restore
			preserve
				replace `x' = 0
				predict yhat, xb
				su yhat
			restore
			local y0 = r(mean)
			preserve
				replace `x' = 1
				predict yhat, xb
				su yhat
			restore
			local y1 = r(mean)
			use tmp0, clear
			replace B = exp(`y1')-exp(`y0') if _n == `i'
			save tmp0, replace
			nois _dots `i' 0
		}
		su B
		use tmp0, clear
		erase tmp0.dta
		drop B
		replace ESE`model' = r(sd) if _n == `model'

	
	}
	
end

*** covariates ***

local X1 totalrevownsourcespcp pcttotaltax pcttotalig
local X2 lpopulation popdensity incomepcp pctba pct65plus H pcthisp herf pctforeign
local X3 crimepcp totalpolicepcp dem ngovspcp netmigration 

local X `X1' `X2' `X3' `X4'

foreach x in `X' pctblack {
	su `x'
	replace `x' = (`x' - r(min)) / (r(max) - r(min))
	drop if mi(`x')
}

*** do the estimates ***

local nrep 500

reg lfinesandforfeitspcp pctblack, robust
myest 1 pctblack `nrep'

reg lfinesandforfeitspcp pctblack `X1', robust
myest 2 pctblack `nrep'

reg lfinesandforfeitspcp pctblack `X1' `X2', robust
myest 3 pctblack `nrep'

reg lfinesandforfeitspcp pctblack `X1' `X2' `X3', robust
myest 4 pctblack `nrep'

collapse B* SE* EB* ESE*

gen index = _n
reshape i index
reshape j model
reshape xi
reshape xij B SE EB ESE
reshape long
drop index model

gen myp = 2*(1-normal(abs(B/SE)))
gen mystars = ""
replace mystars = mystars + "*"  if myp < 0.05
replace mystars = mystars + "*"  if myp < 0.01
replace mystars = mystars + "*"  if myp < 0.001

foreach x in B SE EB ESE {
	gen `x'x = string(`x', "%20.02fc")
	drop `x'
	rename `x'x `x'
}
foreach x in SE ESE {
	replace `x' = "(" + `x' + ")"
}
replace B = B + mystars
drop mystars myp

sxpose, clear force

expand 3

forvalues i = 1(1)4 {
	replace _var`i' = _var`i'[_n-1] if _n == 5 
	replace _var`i' = _var`i'[_n-1] if _n == 4
	replace _var`i' = " " if _n == 3 | _n >= 6
}

gen mylab = " "
order mylab
replace mylab = "Percent black population" if _n == 1
replace mylab = "Effect size (\\$)" if _n == 4
replace mylab = "Controls:" if _n == 6
replace mylab = "Local finances" if _n == 7
replace mylab = "Demographics" if _n == 8
replace mylab = "Crime, fragmentation, mobility, Dem vote" if _n == 9

replace _var2 = "\checkmark" if inlist(mylab, "Local finances")
replace _var3 = "\checkmark" if inlist(mylab, "Local finances", "Demographics")
replace _var4 = "\checkmark" if inlist(mylab, "Local finances", "Demographics", "Crime, fragmentation, mobility, Dem vote")

keep if _n < 10

#delimit;

listtex 
	using "table1.tex"
	,
	replace
	type rstyle(tabular)
	head(
		"\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}"
		"\toprule&(1)&(2)&(3)&(4)""\\
		\midrule"
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr
